#include "stdafx.h"
#include "BoxTest.h"
#include "../Graphic/RenderVideo.h"
#include "../Object/RectObject.h"
#include "../Object/CircleObject.h"

CBoxTest::CBoxTest()
{}

CBoxTest::~CBoxTest()
{
	delete m_pGround;
	m_pGround = 0;
	delete m_pBlock;
	m_pBlock = 0;
	delete m_pBlock1;
	m_pBlock1 = 0;
	delete m_pBlock2;
	m_pBlock2 = 0;
	delete m_pWorld;
	m_pWorld = 0;
}


void CBoxTest::Initialize()
{
	b2AABB worldAABB;
	worldAABB.lowerBound.Set(-100.0f, -100.0f);
	worldAABB.upperBound.Set(100.0f, 100.0f);
	b2Vec2 gravity(0.f, -10.f);
	bool doSleep = true;

	m_pWorld= new b2World(worldAABB, gravity, doSleep);

	m_pGround = new CRectObject(m_pWorld);
	m_pGround->Initialize();
	m_pGround->SetRect(core::position2df(0, -10), 50, 10);
	//m_pGround->SetRect(-50, -20, 50, 0);
	m_pGround->SetSolid(true);	
	m_pGround->SetColor(video::SColor(255, 0, 0, 255));
	m_pGround->GenB2();

	m_pBlock = new CRectObject(m_pWorld);
	m_pBlock->Initialize();
	m_pBlock->SetRect(core::position2df(0, 10.f), 0.25, 0.25);
	//m_pBlock->SetRect(-1, 3, 1, 5);	
	m_pBlock->SetDensity(1.0f);
	m_pBlock->SetFriction(0.3f);
	m_pBlock->SetRestitution(0.6f);
	m_pBlock->GenB2();

	m_pBlock1 = new CCircleObject(m_pWorld);
	m_pBlock1->Initialize();
	m_pBlock1->SetCircle(core::position2df(3, 15.f), 0.5);
	m_pBlock1->SetDensity(1.0f);
	m_pBlock1->SetFriction(0.3f);
	m_pBlock1->SetRestitution(0.7f);
	m_pBlock1->GenB2();

	m_pBlock2 = new CCircleObject(m_pWorld);
	m_pBlock2->Initialize();
	m_pBlock2->SetCircle(core::position2df(3.1, 20.f), 1);
	m_pBlock2->SetDensity(1.0f);
	m_pBlock2->SetFriction(0.3f);
	m_pBlock2->SetRestitution(0.7f);
	m_pBlock2->GenB2();
}

void CBoxTest::Update()
{
	float32 steptime = 1.f / 60.f;
	int32 iterations = 10;
	int nn = os::Timer::getRealTime();
	m_pWorld->Step(steptime,iterations);	
}

void CBoxTest::Render()
{
	m_pGround->Draw();
	m_pBlock->Draw();
	m_pBlock1->Draw();
	m_pBlock2->Draw();
	TRACE("pos:%f\n", m_pBlock->m_pB2Body->GetPosition().y);
}
